package org.lcb.demo.shiyan3.controller;

import org.lcb.demo.shiyan3.result.Result;
import org.lcb.demo.shiyan3.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Cart;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @GetMapping()
    public Result<List<Cart>> getCartList(){
        List<Cart> list = cartService.getList();
        return Result.success(list);
    }
    //根据id删除指定的购物车
    @DeleteMapping("/{id}")
    public Result deleteCartById(@PathVariable int id){

        cartService.deleteCartById(id);
        return Result.success();
    }
    //根据指定id更新购物车产品的数量
    @PutMapping()
    public Result updateCartNumById(@RequestBody Cart cart){
        cartService.updateCartNumById(cart.getProductId(),cart.getQuantity());
        return Result.success();
    }
    //往购物车中添加产品;
    @PostMapping()
    public Result insertIntoCart(@RequestBody Cart cart){
//        System.out.println(cart);
        cartService.insertIntoCart(cart.getProductId(),cart.getQuantity());
        return Result.success();
    }



}
